package en.edu.zxj.rabbit.extensions.demo.controller;

import en.edu.zxj.rabbit.extensions.demo.constant.Constants;
import en.edu.zxj.rabbit.extensions.demo.model.StudentInfo;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-09-20
 * @time: 20:51:56
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE_NAME, Constants.ACK_ROUTING_KEY, "ack test");

        return "ack";
    }

    @RequestMapping("student")
    public String student() {
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setName("张三");
        studentInfo.setAge(18);
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE_NAME, Constants.ACK_ROUTING_KEY, studentInfo);
        return studentInfo.toString();
    }

    @RequestMapping("/confirm")
    public String confirm() {
        CorrelationData correlationData = new CorrelationData("123456");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE_NAME, Constants.CONFIRM_ROUTING_KEY, "confirm test", correlationData);
        return "confirm";
    }

    @RequestMapping("/return")
    public String returnMsg() {
        CorrelationData correlationData = new CorrelationData("654321");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE_NAME, "confirm1", "return test", correlationData);
        return "return";
    }

    @RequestMapping("/retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE_NAME, Constants.RETRY_ROUTING_KEY, "retry test");
        return "retry";
    }

    @RequestMapping("/ttl")
    public String ttl() {
        // 发送带有过期时间的消息
        // String ttlTime = "10000";
        // rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE_NAME, "", "ttl test", new MessagePostProcessor() {
        //     @Override
        //     public Message postProcessMessage(Message message) throws AmqpException {
        //         message.getMessageProperties().setExpiration(ttlTime);
        //         return message;
        //     }
        // });

        // 发送没有过期时间的消息
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE_NAME, "", "ttl test");

        return "ttl";
    }


    @RequestMapping("/dlx")
    public String dlx() {
        // 1. 测试过期时间, 当时间达到TTL, 消息⾃动进⼊到死信队列
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME, Constants.NORMAL_ROUTING_KEY, "dlx test");

        // 2. 测试达到队列⻓度, 消息进⼊死信队列
        // for (int i = 0; i < 20; i++) {
        //     rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME, Constants.NORMAL_ROUTING_KEY, "dlx test" + i);
        // }
        return "dlx";
    }

    @RequestMapping("/delay")
    public String delay() {
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME,
                Constants.NORMAL_ROUTING_KEY, "delay test 10s... " + new Date(), message -> {
                    message.getMessageProperties().setExpiration("10000");
                    return message;
                });

        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME,
                Constants.NORMAL_ROUTING_KEY, "delay test 5s... " + new Date(), message -> {
                    message.getMessageProperties().setExpiration("5000");
                    return message;
                });

        return "delay";
    }

    @RequestMapping("/delay2")
    public String delay2() {
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE_NAME,
                Constants.DELAY_ROUTING_KEY, "delay test 10s... " + new Date(), message -> {
                    message.getMessageProperties().setDelayLong(10_000L);
                    return message;
                });

        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE_NAME,
                Constants.DELAY_ROUTING_KEY, "delay test 5s... " + new Date(), message -> {
                    message.getMessageProperties().setDelayLong(5_000L);
                    return message;
                });

        return "delay2";
    }

    @RequestMapping("/qos")
    public String qos() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("qosExchange", "qos", "qos test" + i);
        }
        return "qos";
    }
}
